package com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad

import android.annotation.SuppressLint
import android.app.Activity
import com.anythink.splashad.api.ATSplashAd
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.ads.admob.open_ad.AppForegroundHelper
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsSwitch
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.listener.ad_action.ATSplashExListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.listener.ad_action.TopOnOpenAdListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.ATAdSourceStatusListenerImpl
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.listener.ad_source.TopOnAdSourceListener
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants

object TopOnOpenAdManager {

    @SuppressLint("StaticFieldLeak")
    var mSplashAd: ATSplashAd? = null
    private var openAdListenerImpl: ATSplashExListenerImpl? = null
    private var sourceStatusListenerImpl: ATAdSourceStatusListenerImpl? = null
    private var topOnOpenAdListener: TopOnOpenAdListener? = null
    fun initTopOnSplashAd() {
//        if (mSplashAd == null) {
//        }
        openAdListenerImpl = ATSplashExListenerImpl()
        mSplashAd = ATSplashAd(
            BpApplication.context,
            Constants.TOP_ON_OPEN_ID,
            openAdListenerImpl,
            9000
        )
        val localMap: Map<String, Any> = HashMap()
        mSplashAd!!.setLocalExtra(localMap)
//            sourceStatusListenerImpl = ATAdSourceStatusListenerImpl()
//            mSplashAd!!.setAdSourceStatusListener(sourceStatusListenerImpl)
        loadAd()
    }

    fun setExListener(topOnOpenAdListener: TopOnOpenAdListener?) {
        this.topOnOpenAdListener = topOnOpenAdListener
        if (openAdListenerImpl == null) {
            openAdListenerImpl = ATSplashExListenerImpl()
        }
        mSplashAd?.setAdListener(openAdListenerImpl)
        openAdListenerImpl?.setListener(topOnOpenAdListener)
    }

    fun setSourceListener(topOnAdSourceListener: TopOnAdSourceListener?) {
//        if (sourceStatusListenerImpl == null) {
//            sourceStatusListenerImpl = ATAdSourceStatusListenerImpl()
//        }
//        mSplashAd?.setAdSourceStatusListener(sourceStatusListenerImpl)
//        sourceStatusListenerImpl?.setSourceStatusListener(topOnAdSourceListener)
    }

    fun loadAd() {
        LogUtil.i("TopOn 【开屏广告】 adOpenControl: ${AdsSwitch.getBoolean(AdsSwitch.adOpenControl)}")
        if (!AdsSwitch.getBoolean(AdsSwitch.adOpenControl)) {
            topOnOpenAdListener?.onFireBaseAdIsClosed()
            return
        }
        mSplashAd?.apply { loadAd() }
    }

    fun isAdReady(): Boolean {
        if (!AdsSwitch.getBoolean(AdsSwitch.adOpenControl)) {
            return false
        }
        if (mSplashAd != null) {
            return mSplashAd!!.isAdReady
        } else {
            LogUtil.iTopOn("TopOn 【开屏广告】 mSplashAd == null activity：${AppForegroundHelper().getCurrentActivity()}")
//            initTopOnSplashAd()
        }
        return false
    }

    fun releaseListener() {
        if (mSplashAd != null) {
            setExListener(null)
            setSourceListener(null)
            mSplashAd!!.setAdListener(null)
            mSplashAd!!.setAdDownloadListener(null)
            mSplashAd!!.setAdSourceStatusListener(null)
            openAdListenerImpl = null
            topOnOpenAdListener = null
            sourceStatusListenerImpl = null
            mSplashAd = null
            LogUtil.iTopOn("开屏对象释放了 mSplashAd = null")
        }
        initTopOnSplashAd()
    }


    fun releaseOpenAd(vararg str: String) {
        if (mSplashAd != null) {
            setExListener(null)
            setSourceListener(null)
            mSplashAd!!.setAdListener(null)
            mSplashAd!!.setAdDownloadListener(null)
            mSplashAd!!.setAdSourceStatusListener(null)
            openAdListenerImpl = null
            topOnOpenAdListener = null
            sourceStatusListenerImpl = null
            mSplashAd = null
        }
    }
}
